﻿using System;
using System.Runtime.InteropServices;

namespace ActiveDesktop
{
    public static class MouseEvents
    {
        // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260.aspx
        [DllImport("user32.dll", EntryPoint = "mouse_event")]
        private static extern void RaiseInternal(MouseEventType eventType, uint positionX, uint positionY, uint eventData, UIntPtr extraInfo);

        public static void RaiseMouseMove(uint positionX = 0, uint positionY = 0)
        {
            MouseEvents.RaiseEvent(MouseEventType.Move, positionX: positionX, positionY: positionY);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="movementNumber">
        /// Specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward,
        /// away from the user; a negative value indicates that the wheel was rotated backward, toward the user.
        /// </param>
        public static void RaiseWheelMove(short movementNumber)
        {
            MouseEvents.RaiseEvent(MouseEventType.Wheel, eventData: (uint)movementNumber * 120);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="movementNumber">
        /// Specifies the amount of wheel movement.
        /// A positive value indicates that the wheel was tilted to the right
        /// A negative value indicates that the wheel was tilted to the left
        /// </param>
        public static void RaiseHorizontalWheelMove(short movementNumber)
        {
            MouseEvents.RaiseEvent(MouseEventType.HWheel, eventData: (uint)movementNumber);
        }
        public static void RaiseLeftPressed()
        {
            MouseEvents.RaiseEvent(MouseEventType.LeftDown);
        }
        public static void RaiseLeftReleased()
        {
            MouseEvents.RaiseEvent(MouseEventType.LeftUp);
        }
        public static void RaiseRightPressed()
        {
            MouseEvents.RaiseEvent(MouseEventType.RightDown);
        }
        public static void RaiseRightReleased()
        {
            MouseEvents.RaiseEvent(MouseEventType.RightUp);
        }
        public static void RaiseX1Pressed()
        {
            MouseEvents.RaiseEvent(MouseEventType.XDown, eventData: (uint)XButtonType.XButton1);
        }
        public static void RaiseX1Released(XButtonType button)
        {
            MouseEvents.RaiseEvent(MouseEventType.XUp, eventData: (uint)XButtonType.XButton1);
        }
        public static void RaiseX2Pressed()
        {
            MouseEvents.RaiseEvent(MouseEventType.XDown, eventData: (uint)XButtonType.XButton2);
        }
        public static void RaiseX2Released(XButtonType button)
        {
            MouseEvents.RaiseEvent(MouseEventType.XUp, eventData: (uint)XButtonType.XButton2);
        }
        public static void RaiseEvent(MouseEventType eventType, uint positionX = 0, uint positionY = 0, uint eventData = 0, UIntPtr? extraInfo = null)
        {
            if (!extraInfo.HasValue)
                extraInfo = UIntPtr.Zero;

            MouseEvents.RaiseInternal(eventType, positionX, positionY, eventData, extraInfo.Value);
        }
    }
}
